package com.example.fruit;

import java.util.HashMap;
import java.util.Map;

public class FruitFlyweightFactory {
    private static final Map<String, Fruit> fruitPool = new HashMap<>();

    public static Fruit getFruit(String key) {
        Fruit fruit = fruitPool.get(key);
        if (fruit == null) {
            switch(key) {
                case "A":
                    fruit = new Apple();
                    break;
                case "B":
                    fruit = new Banana();
                    break;
                default:
                    return null;
            }
            fruitPool.put(key, fruit);
            System.out.println("创建新实例: " + key);
        }
        return fruit;
    }

    public static int getPoolSize() {
        return fruitPool.size();
    }
}